/*
 * Copyright (c) 2011-2014, fortiss GmbH.
 * Licensed under the Apache License, Version 2.0.
 *
 * Use, modification and distribution are subject to the terms specified
 * in the accompanying license file LICENSE.txt located at the root directory
 * of this software distribution. A copy is available at
 * http://chromosome.fortiss.org/.
 *
 * This file is part of CHROMOSOME.
 *
 * $Id: loginManagerProcessResponseFunction.c 7837 2014-03-14 12:33:13Z wiesmueller $
 */

/**
 * \file
 *         Source file for function loginManagerProcessResponse in component loginManager.
 *
 * \author
 *         This file has been generated by the CHROMOSOME Modeling Tool (XMT)
 *         (fortiss GmbH).
 */

/******************************************************************************/
/***   Includes                                                             ***/
/******************************************************************************/
#include "xme/core/login/include-gen/loginManagerProcessResponseFunction.h"

#include "xme/core/login/include-gen/loginManagerProcessResponseFunctionWrapper.h"
#include "xme/core/login/include-gen/loginManagerComponent.h"
#include "xme/core/login/include-gen/loginManagerComponentWrapper.h"
#include "xme/core/login/include-gen/loginManagerManifest.h"

#include "xme/core/logUtils.h"

#include "xme/hal/include/mem.h"

// PROTECTED REGION ID(XME_CORE_LOGIN_LOGINMANAGERPROCESSRESPONSEFUNCTION_C_INCLUDES) ENABLED START
#include "xme/core/login/include/loginManager.h"
#include "xme/hal/include/net.h"
#include "xme/wp/waypointConfigInfrastructure.h"
#include <inttypes.h>
// PROTECTED REGION END

/******************************************************************************/
/***   Definitions                                                          ***/
/******************************************************************************/

// PROTECTED REGION ID(XME_CORE_LOGIN_LOGINMANAGERPROCESSRESPONSEFUNCTION_C_DEFINITIONS) ENABLED START

// PROTECTED REGION END

/******************************************************************************/
/***   Variables                                                            ***/
/******************************************************************************/
/**
 * \brief  Variable holding the value of the required output port 'outLoginResponse'.
 *
 * \details If necessary initialize this in the init function.
 *          The value of this variable will be written to the port at the end of
 *          the step function.
 */
static xme_core_topic_login_loginResponse_t
portOutLoginResponseData;

// PROTECTED REGION ID(XME_CORE_LOGIN_LOGINMANAGERPROCESSRESPONSEFUNCTION_C_VARIABLES) ENABLED START

// PROTECTED REGION END

/******************************************************************************/
/***   Prototypes                                                           ***/
/******************************************************************************/

// PROTECTED REGION ID(XME_CORE_LOGIN_LOGINMANAGERPROCESSRESPONSEFUNCTION_C_PROTOTYPES) ENABLED START

// PROTECTED REGION END

/******************************************************************************/
/***   Implementation                                                       ***/
/******************************************************************************/
xme_status_t
xme_core_login_loginManagerProcessResponseFunction_init
(
    xme_core_login_loginManagerComponent_config_t* const componentConfig
)
{
    // PROTECTED REGION ID(XME_CORE_LOGIN_LOGINMANAGERPROCESSRESPONSEFUNCTION_INITIALIZE_C) ENABLED START

    // Nothing to do

    XME_UNUSED_PARAMETER(componentConfig);

    return XME_STATUS_SUCCESS;

    // PROTECTED REGION END
}

void
xme_core_login_loginManagerProcessResponseFunction_step
(
    xme_core_login_loginManagerComponent_config_t* const componentConfig
)
{
    xme_status_t status[1];
    
    xme_core_topic_login_pnpLoginResponse_t portInPnPLoginResponseData; // Required port.
    xme_core_topic_login_loginResponse_t* portOutLoginResponseDataPtr = &portOutLoginResponseData;
    
    (void)xme_hal_mem_set(&portInPnPLoginResponseData, 0u, sizeof(xme_core_topic_login_pnpLoginResponse_t));
    
    status[0] = xme_core_login_loginManagerComponentWrapper_readPortInPnPLoginResponse(&portInPnPLoginResponseData);
    
    {
        // PROTECTED REGION ID(XME_CORE_LOGIN_LOGINMANAGERPROCESSRESPONSEFUNCTION_STEP_C) ENABLED START
        xme_com_interface_address_t interfaceAddress;
        char strIPAddress[XME_COM_INTERFACE_IPV4_STRING_BUFFER_SIZE];

        XME_UNUSED_PARAMETER(componentConfig);
        XME_CHECK_MSG
        (
            XME_STATUS_SUCCESS == status[0],
            XME_CHECK_RVAL_VOID,
            XME_LOG_WARNING,
            "[LoginManager] Read of port inPnPLoginResponse failed.\n"
        );

        XME_CHECK_MSG
        (
            XME_STATUS_SUCCESS == xme_com_interface_ipv4ToGenericAddress
            (
                portInPnPLoginResponseData.ipAddress,
                portInPnPLoginResponseData.ipAddress >> 8,
                portInPnPLoginResponseData.ipAddress >> 16,
                portInPnPLoginResponseData.ipAddress >> 24,
                xme_hal_net_ntohs(portInPnPLoginResponseData.portAddress),
                &interfaceAddress
            ),
            XME_CHECK_RVAL_VOID,
            XME_LOG_ERROR,
            "[LoginManager] Cannot get the interface address from the login request.\n"
        );

        XME_CHECK_MSG
        (
            XME_STATUS_SUCCESS == xme_com_interface_genericAddressToIPv4String
            (
                &interfaceAddress, strIPAddress, XME_COM_INTERFACE_IPV4_STRING_BUFFER_SIZE
            ),
            XME_CHECK_RVAL_VOID,
            XME_LOG_ERROR,
            "Cannot transform ip address to string.\n"
        );

        XME_LOG
        (
            XME_LOG_DEBUG,
            "[LoginManager] Received a PnP Login Response for node %" PRIu16 " and IP address %s \n",
            portInPnPLoginResponseData.nodeId,
            strIPAddress
        );

        XME_CHECK_MSG
        (
            XME_STATUS_SUCCESS == xme_core_login_loginManager_loginResponse(&portInPnPLoginResponseData, portOutLoginResponseDataPtr),
            XME_CHECK_RVAL_VOID,
            XME_LOG_ERROR,
            "The return value for login response was unsuccess"
        );

        {
            xme_com_interface_address_t localInterfaceAddress;
            uint32_t localIP = 0;
            uint16_t localPort = 0;
            status[0] = xme_core_node_getInterface(&localInterfaceAddress);
            XME_CHECK_MSG
            (
                XME_STATUS_SUCCESS == status[0],
                XME_CHECK_RVAL_VOID,
                XME_LOG_ERROR,
                "Could not locate self IP address"
            );

            XME_CHECK_MSG
            (
                XME_STATUS_SUCCESS == xme_com_interface_genericAddressToIPv4(&localInterfaceAddress, &localIP, &localPort),
                XME_CHECK_RVAL_VOID,
                XME_LOG_ERROR,
                "Cannot transform local addres to interface address"
            );

            XME_CHECK_MSG
            (
                XME_STATUS_SUCCESS == xme_wp_wci_addConfig(0, localPort,
                                                               portOutLoginResponseDataPtr->channelID, XME_CORE_TOPIC_PNP_COMPONENTINSTANCEMANIFEST),
                XME_CHECK_RVAL_VOID,
                XME_LOG_ERROR,
                "The return value for adding the configuration for component instance manifest was unsuccess.\n"
            );
        }

        XME_CHECK_MSG
        (
            XME_STATUS_SUCCESS == xme_com_interface_ipv4ToGenericAddress
            (
                portOutLoginResponseDataPtr->ipAddress,
                portOutLoginResponseDataPtr->ipAddress >> 8,
                portOutLoginResponseDataPtr->ipAddress >> 16,
                portOutLoginResponseDataPtr->ipAddress >> 24,
                xme_hal_net_ntohs(portOutLoginResponseDataPtr->portAddress),
                &interfaceAddress
            ),
            XME_CHECK_RVAL_VOID,
            XME_LOG_ERROR,
            "Cannot get the interface address from the login request.\n"
        );

        XME_CHECK_MSG
        (
            XME_STATUS_SUCCESS == xme_com_interface_genericAddressToIPv4String
            (
                &interfaceAddress, strIPAddress, XME_COM_INTERFACE_IPV4_STRING_BUFFER_SIZE
            ),
            XME_CHECK_RVAL_VOID,
            XME_LOG_ERROR,
            "Cannot transform ip address to string.\n"
        );

        XME_LOG
        (
            XME_LOG_NOTE,
            "[LoginManager] Sending Login Response to node %d.\n",
            portOutLoginResponseDataPtr->nodeId,
            portOutLoginResponseDataPtr->guid,
            strIPAddress,
            portOutLoginResponseDataPtr->channelID,
            portOutLoginResponseDataPtr->loginStatus
        );

        // PROTECTED REGION END
    }
    
    status[0] = xme_core_login_loginManagerComponentWrapper_writePortOutLoginResponse(portOutLoginResponseDataPtr);
    
    {
        // PROTECTED REGION ID(XME_CORE_LOGIN_LOGINMANAGERPROCESSRESPONSEFUNCTION_STEP_2_C) ENABLED START

        XME_CHECK_MSG
        (
            XME_STATUS_SUCCESS == status[0],
            XME_CHECK_RVAL_VOID,
            XME_LOG_WARNING,
            "The write port operation failed."
        );

        // PROTECTED REGION END
    }
}

void
xme_core_login_loginManagerProcessResponseFunction_fini
(
    xme_core_login_loginManagerComponent_config_t* const componentConfig
)
{
    // PROTECTED REGION ID(XME_CORE_LOGIN_LOGINMANAGERPROCESSRESPONSEFUNCTION_TERMINATE_C) ENABLED START

    // Nothing to do

    XME_UNUSED_PARAMETER(componentConfig);

    // PROTECTED REGION END
}

// PROTECTED REGION ID(XME_CORE_LOGIN_LOGINMANAGERPROCESSRESPONSEFUNCTION_IMPLEMENTATION_C) ENABLED START

// PROTECTED REGION END
